import { mongo } from "../mogoConfig";

// 数组查询
const db = mongo.db("my_test")

const run = async () => {
    const cursor1 = db.collection("inventory")
        .find({ tags: ["red", "blank"] } //按指定顺序恰好有两个
        )
    console.log('查询1-------------------')
    for await (const item of cursor1) {
        console.log(JSON.stringify(item))
    }

    const cursor2 = db.collection("inventory")
        .find({ tags: ["blank", "red"] } //按指定顺序恰好有两个
        );
    console.log('查询2-------------------')
    for await (const item of cursor2) {
        console.log(JSON.stringify(item))
    }

    const cursor3 = db.collection("inventory")
        .find({ tags: { $all: ["red"] } } //不指定顺序有1个
        );
    console.log('查询3-------------------')
    for await (const item of cursor3) {
        console.log(JSON.stringify(item))
    }

    const cursor4 = db.collection("inventory")
        .find({ tags: 'red' } //包含至少一个
        );
    console.log('查询4-------------------')
    for await (const item of cursor4) {
        console.log(JSON.stringify(item))
    }
}

run()
    .finally(() => {
        mongo.close()
    })



console.log("执行结束")